package com.itheima.health.servcie.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.health.mapper.CheckItemMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService  {
    @Autowired
    private CheckItemMapper checkItemMapper;

    @Override
    public void add(CheckItem checkItem) {
        checkItemMapper.add(checkItem);
    }


    //检查项分页查询
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //完成分页查询,基于mybatis框架提供的分页助手进行查询
        PageHelper.startPage(currentPage,pageSize);
        //只用先前面的    limit调用的是插件  传入currentPage 和pagesize
        //select * from t_checkitem limit 0,10

        Page<CheckItem> page = checkItemMapper.selectByCondition(queryString);
        long total = page.getTotal();
        List<CheckItem> rows = page.getResult();


        return new PageResult(total,rows);
    }


    //根据id 删除检查项
    @Override
    public void deleteById(Integer id) {
       //先判断当前检查项 是否关联到检查组
        long count = checkItemMapper.findCountByCheckItemId(id);
        if(count>0){
            //不允许删除  抛出一个异常
            new RuntimeException();
        }
        checkItemMapper.deleteById(id);



    }

    @Override
    public void edit(CheckItem checkItem) {

        checkItemMapper.edit(checkItem);

    }

    //回显数据
    @Override
    public CheckItem findById(Integer id) {
        CheckItem checkItem = checkItemMapper.findById(id);
        return checkItem;

    }

    @Override
    public List<CheckItem> findAll() {

        return checkItemMapper.findAll();
    }


}
